from AR.models.t2s_lightning_module import Text2SemanticLightningModule
import torch
from .DataStruct import DeviceType


class GPT:
    def __init__(self, is_half=False, device=DeviceType.CPU):
        self.isHalf = is_half
        self.device = device.value
        pass

    def change_gpt_weights(self, gpt_path):
        self.hz = 50
        dict_s1 = torch.load(gpt_path, map_location="cpu")
        config = dict_s1["config"]
        self.max_sec = config["data"]["max_sec"]
        self.t2s_model = Text2SemanticLightningModule(config, "****", is_train=False)
        self.t2s_model.load_state_dict(dict_s1["weight"])
        if self.isHalf == True:
            self.t2s_model = self.t2s_model.half()
        self.t2s_model = self.t2s_model.to(self.device)
        self.t2s_model.eval()
        total = sum([param.nelement() for param in self.t2s_model.parameters()])
        print("Number of parameter: %.2fM" % (total / 1e6))
